
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>逐行解读effect如何注册副作用函数 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="4_cleanup_algorithm.html" />
    
    
    <link rel="prev" href="2_reactive_map_set.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../1_online_rendering/0_intro.html">
            
                <a href="../1_online_rendering/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1_online_rendering/1_start_from_static_view.html">
            
                <a href="../1_online_rendering/1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                <a href="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../1_online_rendering/4_directives/2_event_binding.html">
            
                <a href="../1_online_rendering/4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                <a href="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../1_online_rendering/4_directives/4_ref.html">
            
                <a href="../1_online_rendering/4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../1_online_rendering/5_tips_for_template.html">
            
                <a href="../1_online_rendering/5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="1_reactive_object_array.html">
            
                <a href="1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="2_reactive_map_set.html">
            
                <a href="2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="3_register_side_effect.html">
            
                <a href="3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="4_cleanup_algorithm.html">
            
                <a href="4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="5_smi.html">
            
                <a href="5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="6_trigger_and_scheduler.html">
            
                <a href="6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >逐行解读effect如何注册副作用函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;"><b>1. </b>&#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</a></li><ul><li><span class="title-icon "></span><a href="#reactiveeffectrunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><b>1.1. </b>ReactiveEffectRunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#reactiveeffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><b>1.2. </b>ReactiveEffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#&#x4EC0;&#x4E48;&#x662F;effectscope&#xFF1F;"><b>1.3. </b>&#x4EC0;&#x4E48;&#x662F;EffectScope&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#&#x5C0F;&#x7ED3;"><b>1.4. </b>&#x5C0F;&#x7ED3;</a></li></ul></ul></div><h1 id="&#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;"><a name="&#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x9010;&#x884C;&#x89E3;&#x8BFB;effect&#x5982;&#x4F55;&#x6CE8;&#x518C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</h1>
<p>&#x5F53;&#x6211;&#x4EEC;&#x901A;&#x8FC7;<code>effect</code>&#x5C06;&#x526F;&#x51FD;&#x6570;&#x5411;&#x54CD;&#x5E94;&#x4E0A;&#x4E0B;&#x6587;&#x6CE8;&#x518C;&#x540E;&#xFF0C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5185;&#x8BBF;&#x95EE;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x65F6;&#x5373;&#x4F1A;&#x81EA;&#x52A8;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#xFF0C;&#x5E76;&#x5728;&#x76F8;&#x5E94;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5C5E;&#x6027;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x540E;&#xFF0C;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-ts">// ./effect.ts

export function effect&lt;T = any&gt;(
  fn: () =&gt; T,
  options?: ReactiveEffectOptions
): ReactiveEffectRunner {
  if ((fn as ReactiveEffectRunner).effect) {
    fn = (fn as ReactiveEffectRunner).effect.fn
  }

  const _effect = new ReactiveEffect(fn)
  if (options) {
    extend(_effect, options)
    if (options.scope) recordEffectScope(_effect, options.scope)
  }
  // &#x9ED8;&#x8BA4;&#x662F;&#x9A6C;&#x4E0A;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#xFF0C;&#x4F46;&#x53EF;&#x901A;&#x8FC7;lazy&#x5C5E;&#x6027;&#x5EF6;&#x8FDF;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#xFF0C;&#x5EF6;&#x8FDF;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#x3002;
  if (!options || !options.lazy) {
    _effect.run()
  }
  // &#x7C7B;&#x578B;&#x4E3A;ReactiveEffectRunner&#x7684;runner&#x662F;&#x4E00;&#x4E2A;&#x7ED1;&#x5B9A;this&#x7684;&#x51FD;&#x6570;
  const runner = _effect.run.bind(_effect) as ReactiveEffectRunner
  runner.effect = _effect
  return runner
}
</code></pre>
<p><code>effect</code>&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x5341;&#x5206;&#x5C11;&#xFF0C;&#x4E3B;&#x8981;&#x6D41;&#x7A0B;&#x662F;</p>
<ol>
<li>&#x5C06;&#x57FA;&#x4E8E;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6784;&#x5EFA;<code>ReactiveEffect</code>&#x5BF9;&#x8C61;</li>
<li>&#x82E5;&#x4E3A;&#x9ED8;&#x8BA4;&#x6A21;&#x5F0F;&#x5219;&#x9A6C;&#x4E0A;&#x8C03;&#x7528;<code>ReactiveEffect</code>&#x5BF9;&#x8C61;&#x7684;<code>run</code>&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x3002;</li>
</ol>
<p>&#x4E0D;&#x8FC7;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x6709;&#x51E0;&#x4E2A;&#x7591;&#x95EE;</p>
<ol>
<li><code>ReactiveEffectRunner</code>&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</li>
<li><code>ReactiveEffect</code>&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;&#x663E;&#x7136;<code>ReactiveEffect</code>&#x7684;<code>run</code>&#x65B9;&#x6CD5;&#x624D;&#x662F;&#x68A6;&#x5F00;&#x59CB;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5230;&#x5E95;&#x5B83;&#x505A;&#x4E86;&#x4E9B;&#x4EC0;&#x4E48;&#xFF1F;</li>
<li>&#x9488;&#x5BF9;&#x914D;&#x7F6E;&#x9879;<code>scope</code>&#xFF0C;<code>recordEffectScope</code>&#x7684;&#x4F5C;&#x7528;&#xFF1F;</li>
</ol>
<h2 id="reactiveeffectrunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><a name="reactiveeffectrunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#reactiveeffectrunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. ReactiveEffectRunner&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</h2>
<pre><code class="lang-ts"><span class="hljs-comment">// ./effect.ts</span>

<span class="hljs-comment">// ReactiveEffectRunner&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x4E14;&#x6709;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;effect&#x7684;&#x5C5E;&#x6027;&#x4E14;&#x5176;&#x7C7B;&#x578B;&#x4E3A;ReactiveEffect</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> ReactiveEffectRunner&lt;T = any&gt; {
  (): T
  effect: ReactiveEffect
}
</code></pre>
<h2 id="reactiveeffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><a name="reactiveeffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#reactiveeffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. ReactiveEffect&#x751F;&#x6210;&#x7684;&#x5BF9;&#x8C61;&#x7A76;&#x7ADF;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</h2>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x7528;&#x4E8E;&#x8BB0;&#x5F55;&#x4F4D;&#x4E8E;&#x54CD;&#x5E94;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;effect&#x5D4C;&#x5957;&#x5C42;&#x6B21;&#x6570;</span>
<span class="hljs-keyword">let</span> effectTrackDepth = <span class="hljs-number">0</span>
<span class="hljs-comment">// &#x4E8C;&#x8FDB;&#x5236;&#x4F4D;&#xFF0C;&#x6BCF;&#x4E00;&#x4F4D;&#x7528;&#x4E8E;&#x6807;&#x8BC6;&#x5F53;&#x524D;effect&#x5D4C;&#x5957;&#x5C42;&#x7EA7;&#x7684;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#x7684;&#x542F;&#x7528;&#x72B6;&#x6001;</span>
<span class="hljs-keyword">export</span> left trackOpBit = <span class="hljs-number">1</span>
<span class="hljs-comment">// &#x8868;&#x793A;&#x6700;&#x5927;&#x6807;&#x8BB0;&#x7684;&#x4F4D;&#x6570;</span>
<span class="hljs-keyword">const</span> maxMarkerBits = <span class="hljs-number">30</span>

<span class="hljs-keyword">const</span> effectStack: ReactiveEffect[] = []
<span class="hljs-keyword">let</span> activeEffect: ReactiveEffect | <span class="hljs-literal">undefined</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> ReactiveEffect&lt;T = <span class="hljs-built_in">any</span>&gt; {
  <span class="hljs-comment">// &#x7528;&#x4E8E;&#x6807;&#x8BC6;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x662F;&#x5426;&#x4F4D;&#x4E8E;&#x54CD;&#x5E94;&#x5F0F;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x88AB;&#x6267;&#x884C;</span>
  active = <span class="hljs-literal">true</span>
  <span class="hljs-comment">// &#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6301;&#x6709;&#x5B83;&#x6240;&#x5728;&#x7684;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x96C6;&#x5408;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x7528;&#x4E8E;&#x4ECE;&#x8FD9;&#x4E9B;&#x4F9D;&#x8D56;&#x96C6;&#x5408;&#x5220;&#x9664;&#x81EA;&#x8EAB;</span>
  deps: Dep[] = []
  <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x4E3A;false&#xFF0C;&#x800C;true&#x8868;&#x793A;&#x82E5;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x9047;&#x5230;`foo.bar += 1`&#x5219;&#x65E0;&#x9650;&#x9012;&#x5F52;&#x6267;&#x884C;&#x81EA;&#x8EAB;&#xFF0C;&#x76F4;&#x5230;&#x7206;&#x6808;</span>
  allowRecurse?: <span class="hljs-built_in">boolean</span>

  <span class="hljs-keyword">constructor</span>(
    public fn: () =&gt; T,
    public scheduler: EffectScheduler | null = null,
    scope?: EffectScope | null
  ) {
    recordEffectScope(<span class="hljs-keyword">this</span>, scope)
  }

  run() {
    <span class="hljs-comment">/**
     * &#x82E5;&#x5F53;&#x524D;ReactiveEffect&#x5BF9;&#x8C61;&#x8131;&#x79BB;&#x54CD;&#x5E94;&#x5F0F;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x90A3;&#x4E48;&#x5176;&#x5BF9;&#x5E94;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x88AB;&#x6267;&#x884C;&#x65F6;&#x4E0D;&#x4F1A;&#x518D;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#xFF0C;&#x5E76;&#x4E14;&#x5176;&#x5185;&#x90E8;&#x8BBF;&#x95EE;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x4E5F;&#x4F1A;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x8BE5;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;
     */</span>
    <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.active) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.fn()
    }
    <span class="hljs-comment">// &#x82E5;&#x53C2;&#x4E0E;&#x54CD;&#x5E94;&#x5F0F;&#x4E0A;&#x4E0B;&#x6587;&#x5219;&#x9700;&#x8981;&#x5148;&#x538B;&#x6808;</span>
    <span class="hljs-keyword">if</span> (!effectStack.includes(<span class="hljs-keyword">this</span>)) {
      <span class="hljs-keyword">try</span> {
        <span class="hljs-comment">// &#x538B;&#x6808;&#x7684;&#x540C;&#x65F6;&#x5FC5;&#x987B;&#x5C06;&#x5F53;&#x524D;ReactiveEffect&#x5BF9;&#x8C61;&#x8BBE;&#x7F6E;&#x4E3A;&#x6D3B;&#x8DC3;&#xFF0C;&#x5373;&#x7A0B;&#x5E8F;&#x6808;&#x4E2D;&#x5F53;&#x524D;&#x6808;&#x5E27;&#x7684;&#x610F;&#x4E49;&#x3002;</span>
        effectStack.push(activeEffect = <span class="hljs-keyword">this</span>)
        enableTracking()

        trackOpBit = <span class="hljs-number">1</span> &lt;&lt; ++effectTrackDepth

        <span class="hljs-keyword">if</span> (effectTrackDepth &lt;= maxMarkerBits) {
          <span class="hljs-comment">// &#x6807;&#x8BB0;&#x5DF2;&#x8DDF;&#x8E2A;&#x8FC7;&#x7684;&#x4F9D;&#x8D56;</span>
          initDepMarkers(<span class="hljs-keyword">this</span>)
        }
        <span class="hljs-keyword">else</span> {
          cleanupEffect(<span class="hljs-keyword">this</span>)
        }

        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.fn()
      }
      <span class="hljs-keyword">finally</span> {
        <span class="hljs-keyword">if</span> (effectTrackDepth &lt;= maxMarkerBits) {
          <span class="hljs-comment">/**
           * &#x7528;&#x4E8E;&#x5BF9;&#x66FE;&#x7ECF;&#x8DDF;&#x8E2A;&#x8FC7;&#xFF0C;&#x4F46;&#x672C;&#x6B21;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x65F6;&#x6CA1;&#x6709;&#x8DDF;&#x8E2A;&#x7684;&#x4F9D;&#x8D56;&#xFF0C;&#x91C7;&#x53D6;&#x5220;&#x9664;&#x64CD;&#x4F5C;&#x3002;
           * &#x5373;&#xFF0C;&#x65B0;&#x8DDF;&#x8E2A;&#x7684; &#x548C; &#x672C;&#x8F6E;&#x8DDF;&#x8E2A;&#x8FC7;&#x7684;&#x90FD;&#x4F1A;&#x88AB;&#x4FDD;&#x7559;&#x3002;
           */</span>
          finalizeDepMarkers(<span class="hljs-keyword">this</span>)
        }

        trackOpBit = <span class="hljs-number">1</span> &lt;&lt; --effectTrackDepth
        resetTracking()
        <span class="hljs-comment">// &#x6700;&#x540E;&#x5F53;&#x7136;&#x5F39;&#x6808;&#xFF0C;&#x628A;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x8FD8;&#x7ED9;&#x4E0A;&#x4E00;&#x4E2A;&#x6808;&#x5E27;&#x54AF;</span>
        effectStack.pop()
        <span class="hljs-keyword">const</span> n = effectStack.length
        activeEffect = n &gt; <span class="hljs-number">0</span> ? effectStack[n - <span class="hljs-number">1</span>] : <span class="hljs-literal">undefined</span>  
      }
    }

    <span class="hljs-comment">/**
     * &#x8BA9;&#x5F53;&#x524D;ReactiveEffect&#x5BF9;&#x8C61;&#x8131;&#x79BB;&#x54CD;&#x5E94;&#x5F0F;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x8BF7;&#x8BB0;&#x4F4F;&#x8FD9;&#x662F;&#x4E00;&#x53BB;&#x4E0D;&#x56DE;&#x5934;&#x7684;&#x64CD;&#x4F5C;&#x54E6;&#xFF01;
     */</span> 
    stop() {
      <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.active) {
        cleanupEffect(<span class="hljs-keyword">this</span>)
        <span class="hljs-keyword">this</span>.active = <span class="hljs-literal">false</span>
      }
    }
  }
}
</code></pre>
<p>&#x4E3A;&#x5E94;&#x5BF9;&#x5D4C;&#x5957;<code>effect</code>&#x5185;&#x90E8;&#x5C06;&#x5F53;&#x524D;&#x4F4D;&#x4E8E;&#x54CD;&#x5E94;&#x4E0A;&#x4E0B;&#x6587;&#x7684;ReactiveEffect&#x5BF9;&#x8C61;&#x538B;&#x5165;&#x6808;&#x7ED3;&#x6784;<code>effectStack: ReactiveEffect[]</code>&#xFF0C;&#x5F53;&#x5F53;&#x524D;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x540E;&#x518D;&#x5F39;&#x51FA;&#x6808;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x867D;&#x7136;&#x6211;&#x4EEC;&#x901A;&#x8FC7;<code>effect</code>&#x51FD;&#x6570;&#x5C06;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6CE8;&#x518C;&#x5230;&#x54CD;&#x5E94;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#xFF0C;&#x4F46;&#x6211;&#x4EEC;&#x4ECD;&#x80FD;&#x901A;&#x8FC7;&#x8C03;&#x7528;<code>stop</code>&#x65B9;&#x6CD5;&#x8BA9;&#x5176;&#x8131;&#x79BB;&#x54CD;&#x5E94;&#x4E0A;&#x4E0B;&#x6587;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cleanupEffect</span>(<span class="hljs-params">effect: ReactiveEffect</span>) </span>{
  <span class="hljs-keyword">const</span> { deps } = effect
  <span class="hljs-keyword">if</span> (deps.length) {
    <span class="hljs-comment">// &#x5C06;&#x5F53;&#x524D;ReactiveEffect&#x5BF9;&#x8C61;&#x4ECE;&#x5B83;&#x4F9D;&#x8D56;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5C5E;&#x6027;&#x7684;&#x6240;&#x6709;Deps&#x4E2D;&#x5220;&#x9664;&#x81EA;&#x5DF1;&#xFF0C;&#x90A3;&#x4E48;&#x5F53;&#x8FD9;&#x4E9B;&#x54CD;&#x5E94;&#x5F0F;&#x5C5E;&#x6027;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x5219;&#x4E0D;&#x4F1A;&#x904D;&#x5386;&#x5230;&#x5F53;&#x524D;&#x7684;ReactiveEffect&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; deps.length; ++i) {
      deps[i].delete(effect)
    }
    <span class="hljs-comment">// &#x5F53;&#x524D;ReactiveEffect&#x5BF9;&#x8C61;&#x4E0D;&#x518D;&#x53C2;&#x4E0E;&#x4EFB;&#x4F55;&#x54CD;&#x5E94;&#x4E86;</span>
    deps.length = <span class="hljs-number">0</span>
  }
}
</code></pre>
<p>&#x5728;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x524D;&#x548C;&#x6267;&#x884C;&#x540E;&#x6211;&#x4EEC;&#x4F1A;&#x770B;&#x5230;&#x5206;&#x522B;&#x8C03;&#x7528;&#x4E86;<code>enableTracking()</code>&#x548C;<code>resetTracking()</code>&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x8868;&#x793A;<code>enableTracking()</code>&#x6267;&#x884C;&#x540E;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x542F;&#x7528;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#xFF0C;<code>resetTracking()</code>&#x5219;&#x8868;&#x793A;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x5728;&#x6062;&#x590D;&#x4E4B;&#x524D;&#x662F;&#x5426;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x7684;&#x5F00;&#x5173;&#x6267;&#x884C;&#x4E0B;&#x53BB;&#x3002;&#x8981;&#x7406;&#x89E3;&#x5B83;&#x4EEC;&#x5FC5;&#x987B;&#x7ED3;&#x5408;<code>pauseTracking()</code>&#x548C;&#x5B9E;&#x9645;&#x573A;&#x666F;&#x8BF4;&#x660E;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> shouldTrack = <span class="hljs-literal">true</span>
<span class="hljs-keyword">const</span> trackStack: <span class="hljs-built_in">boolean</span>[] = []

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">enableTracking</span>(<span class="hljs-params"></span>) </span>{
  trackStack.push(shouldTrack)
  shouldTrack = <span class="hljs-literal">true</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resetTracking</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> last = trackStack.pop()
  shouldTrack = last === <span class="hljs-literal">undefined</span> ? <span class="hljs-literal">true</span> : last
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pauseTracking</span>(<span class="hljs-params"></span>) </span>{
  trackStack.push(shouldTrack)
  shouldTrack = <span class="hljs-literal">false</span>
}
</code></pre>
<p>&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x5982;&#x4E0B;&#x573A;&#x666F;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> values = reactive([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>])
effect(() =&gt; {
  values.push(<span class="hljs-number">1</span>)
})
</code></pre>
<p>&#x7531;&#x4E8E;&#x5728;&#x6267;&#x884C;<code>push</code>&#x65F6;&#x5185;&#x90E8;&#x4F1A;&#x8BBF;&#x95EE;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;<code>length</code>&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4FEE;&#x6539;<code>length</code>&#x503C;&#xFF0C;&#x56E0;&#x6B64;&#x4F1A;&#x5BFC;&#x81F4;&#x4E0D;&#x65AD;&#x6267;&#x884C;&#x8BE5;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x76F4;&#x5230;&#x629B;&#x51FA;&#x5F02;&#x5E38;<code>Uncaught RangeError: Maximum call stack size exceeded</code>&#xFF0C;&#x5C31;&#x662F;&#x548C;<code>(function error(){ error() })()</code>&#x4E0D;&#x65AD;&#x8C03;&#x7528;&#x81EA;&#x8EAB;&#x5BFC;&#x81F4;&#x6808;&#x7A7A;&#x95F4;&#x4E0D;&#x8DB3;&#x4E00;&#x6837;&#x7684;&#x3002;&#x800C;@vue/reactivity&#x662F;&#x91C7;&#x7528;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x5904;&#x7406;</p>
<pre><code class="lang-ts">;([<span class="hljs-string">&apos;push&apos;</span>, <span class="hljs-string">&apos;pop&apos;</span>, <span class="hljs-string">&apos;shift&apos;</span>, <span class="hljs-string">&apos;unshift&apos;</span>, <span class="hljs-string">&apos;splice&apos;</span>] as <span class="hljs-keyword">const</span>).forEach(key =&gt; {
  instrumentations[key] = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"><span class="hljs-keyword">this</span>: unknown[], ...args: unknown[]</span>) </span>{
    pauseTracking()
    <span class="hljs-keyword">const</span> res = (toRaw(<span class="hljs-keyword">this</span>) as <span class="hljs-built_in">any</span>)[key].apply(<span class="hljs-keyword">this</span>, args)
    resetTracking()
    <span class="hljs-keyword">return</span> res
  }
})
</code></pre>
<p>&#x5373;&#x901A;&#x8FC7;<code>pauseTracking()</code>&#x6682;&#x505C;<code>push</code>&#x5185;&#x90E8;&#x7684;&#x53D1;&#x751F;&#x610F;&#x5916;&#x7684;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#xFF0C;&#x5373;<code>push</code>&#x4EC5;&#x4EC5;&#x4F1A;&#x89E6;&#x53D1;&#x4EE5;&#x5176;&#x4ED6;&#x5F62;&#x5F0F;&#x4F9D;&#x8D56;<code>length</code>&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x3002;&#x7136;&#x540E;&#x901A;&#x8FC7;<code>resetTracking()</code>&#x6062;&#x590D;&#x5230;&#x4E4B;&#x524D;&#x7684;&#x8DDF;&#x8E2A;&#x72B6;&#x6001;&#x3002;</p>
<p>&#x6700;&#x540E;&#x5728;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;<code>return this.fn()</code>&#x524D;&#xFF0C;&#x5C45;&#x7136;&#x6709;&#x51E0;&#x53E5;&#x96BE;&#x4EE5;&#x7406;&#x89E3;&#x7684;&#x8BED;&#x53E5;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">try</span> {
  trackOpBit = <span class="hljs-number">1</span> &lt;&lt; ++effectTrackDepth

  <span class="hljs-keyword">if</span> (effectTrackDepth &lt;= maxMarkerBits) {
    initDepMarkers(<span class="hljs-keyword">this</span>)
  }
  <span class="hljs-keyword">else</span> {
    cleanupEffect(<span class="hljs-keyword">this</span>)
  }

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.fn()
}
<span class="hljs-keyword">finally</span> {
  <span class="hljs-keyword">if</span> (effectTrackDepth &lt;= maxMarkerBits) {
    finalizeDepMarkers(<span class="hljs-keyword">this</span>)
  }

  trackOpBit = <span class="hljs-number">1</span> &lt;&lt; --effectTrackDepth
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x7B80;&#x5316;&#x4E3A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">try</span> {
  cleanupEffect(<span class="hljs-keyword">this</span>)

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.fn()
}
<span class="hljs-keyword">finally</span> {}
</code></pre>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x5728;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x524D;&#x9700;&#x8981;&#x6E05;&#x7406;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8003;&#x8651;&#x4E00;&#x4E0B;&#x5982;&#x4E0B;&#x7684;&#x60C5;&#x51B5;:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> state = reactive({ show: <span class="hljs-literal">true</span>, values: [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] })
effect(() =&gt; {
  <span class="hljs-keyword">if</span> (state.show) {
    <span class="hljs-built_in">console</span>.log(state.values)
  }
})
setTimeout(() =&gt; {
  state.values.push(<span class="hljs-number">4</span>)
}, <span class="hljs-number">5000</span>)

setTimeout(() =&gt; {
  state.show = <span class="hljs-literal">false</span>
}, <span class="hljs-number">10000</span>)

setTimeout(() =&gt; {
  state.values.push(<span class="hljs-number">5</span>)
}, <span class="hljs-number">15000</span>)
</code></pre>
<p>&#x4E00;&#x5F00;&#x59CB;&#x7684;&#x65F6;&#x5019;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5C06;&#x540C;&#x65F6;&#x4F9D;&#x8D56;<code>show</code>&#x548C;<code>values</code>&#xFF0C;5&#x79D2;&#x540E;&#x5411;<code>values</code>&#x8FFD;&#x52A0;&#x65B0;&#x503C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x9A6C;&#x4E0A;&#x88AB;&#x89E6;&#x53D1;&#x91CD;&#x65B0;&#x6267;&#x884C;&#xFF0C;&#x518D;&#x8FC7;10&#x79D2;&#x540E;<code>show</code>&#x8F6C;&#x53D8;&#x4E3A;<code>false</code>&#xFF0C;&#x90A3;&#x4E48;<code>if(state.show)</code>&#x65E0;&#x8BBA;&#x5982;&#x4F55;&#x8FD0;&#x7B97;&#x90FD;&#x4E0D;&#x6210;&#x7ACB;&#xFF0C;&#x6B64;&#x65F6;&#x518D;&#x5BF9;<code>values</code>&#x8FFD;&#x52A0;&#x65B0;&#x503C;&#x82E5;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x518D;&#x6B21;&#x88AB;&#x89E6;&#x53D1;&#x663E;&#x7136;&#x9664;&#x4E86;&#x5360;&#x7528;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x5916;&#xFF0C;&#x522B;&#x65E0;&#x7528;&#x5904;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x524D;&#x90FD;&#x4F1A;&#x5148;&#x6E05;&#x7406;&#x6240;&#x6709;&#x4F9D;&#x8D56;(<code>cleanupEffect</code>&#x7684;&#x4F5C;&#x7528;)&#xFF0C;&#x7136;&#x540E;&#x5728;&#x6267;&#x884C;&#x65F6;&#x91CD;&#x65B0;&#x6536;&#x96C6;&#x3002;</p>
<p>&#x9762;&#x5BF9;&#x4E0A;&#x8FF0;&#x60C5;&#x51B5;&#xFF0C;&#x5148;&#x6E05;&#x7406;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x518D;&#x91CD;&#x65B0;&#x6536;&#x96C6;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x4F46;&#x5982;&#x4E0B;&#x60C5;&#x51B5;&#xFF0C;&#x8FD9;&#x79CD;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#x53CD;&#x800C;&#x589E;&#x52A0;&#x65E0;&#x8C13;&#x7684;&#x6027;&#x80FD;&#x6D88;&#x8017;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> state = reactive({ show: <span class="hljs-literal">true</span>, values: [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] })
effect(() =&gt; {
  <span class="hljs-built_in">console</span>.log(state.values)
})
</code></pre>
<p>&#x90A3;&#x4E48;&#x5E94;&#x8BE5;&#x5982;&#x4F55;&#x5904;&#x7406;&#x63D0;&#x9AD8;&#x624D;&#x80FD;&#x63D0;&#x9AD8;&#x6027;&#x80FD;&#x5462;&#xFF1F;&#x8BF7;&#x770B;&#x4E0B;&#x4E00;&#x8282;<a href="4_cleanup_algorithm.html">&#x300A;&#x4F9D;&#x8D56;&#x6E05;&#x7406;&#x4F18;&#x5316;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;-&#x7B97;&#x6CD5;&#x601D;&#x8DEF;&#x300B;</a>&#x5427;</p>
<h2 id="&#x4EC0;&#x4E48;&#x662F;effectscope&#xFF1F;"><a name="&#x4EC0;&#x4E48;&#x662F;effectscope&#xFF1F;" class="anchor-navigation-ex-anchor" href="#&#x4EC0;&#x4E48;&#x662F;effectscope&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x4EC0;&#x4E48;&#x662F;EffectScope&#xFF1F;</h2>
<p>Vue 3.2&#x5F15;&#x5165;&#x65B0;&#x7684;<em>Effect scope API</em>&#xFF0C;&#x53EF;&#x81EA;&#x52A8;&#x6536;&#x96C6;<code>setup</code>&#x51FD;&#x6570;&#x4E2D;&#x521B;&#x5EFA;&#x7684;<code>effect</code>&#x3001;<code>watch</code>&#x548C;<code>computed</code>&#x7B49;&#xFF0C;&#x5F53;&#x7EC4;&#x4EF6;&#x88AB;&#x9500;&#x6BC1;&#x65F6;&#x81EA;&#x52A8;&#x9500;&#x6BC1;&#x4F5C;&#x7528;&#x57DF;(scope)&#x548C;&#x4F5C;&#x7528;&#x57DF;&#x4E0B;&#x7684;&#x8FD9;&#x4E9B;&#x5B9E;&#x4F8B;(<code>effect</code>&#x3001;<code>watch</code>&#x548C;<code>computed</code>&#x7B49;)&#x3002;&#x8FD9;&#x4E2A;API&#x4E3B;&#x8981;&#x662F;&#x63D0;&#x4F9B;&#x7ED9;&#x63D2;&#x4EF6;&#x6216;&#x5E93;&#x5F00;&#x53D1;&#x8005;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#xFF0C;&#x65E5;&#x5E38;&#x5F00;&#x53D1;&#x4E0D;&#x9700;&#x8981;&#x7528;&#x5230;&#x5B83;&#x3002;</p>
<p>&#x8FD8;&#x8BB0;&#x5F97;petite-vue&#x4E2D;&#x7684;context&#x5417;&#xFF1F;&#x5F53;&#x9047;&#x5230;<code>v-if</code>&#x548C;<code>v-for</code>&#x5C31;&#x4F1A;&#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5206;&#x652F;&#x521B;&#x5EFA;&#x65B0;&#x7684;block&#x5B9E;&#x4F8B;&#x548C;&#x65B0;&#x7684;context&#x5B9E;&#x4F8B;&#xFF0C;&#x800C;&#x5B50;&#x5206;&#x652F;&#x4E0B;&#x7684;&#x6240;&#x6709;<code>ReactiveEffect</code>&#x5B9E;&#x4F8B;&#x90FD;&#x5C06;&#x7EDF;&#x4E00;&#x88AB;&#x5BF9;&#x5E94;&#x7684;context&#x5B9E;&#x4F8B;&#x7BA1;&#x7406;&#xFF0C;&#x5F53;block&#x5B9E;&#x4F8B;&#x88AB;&#x9500;&#x6BC1;&#x5219;&#x4F1A;&#x5BF9;&#x5BF9;&#x5E94;&#x7684;context&#x5B9E;&#x4F8B;&#x4E0B;&#x7684;<code>ReactiveEffect</code>&#x5B9E;&#x4F8B;&#x7EDF;&#x7EDF;&#x9500;&#x6BC1;&#x3002;</p>
<p>block&#x5B9E;&#x4F8B;&#x5BF9;&#x5E94;&#x662F;DOM&#x6811;&#x4E2D;&#x52A8;&#x6001;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x53EF;&#x4EE5;&#x5927;&#x6982;&#x5BF9;&#x5E94;&#x4E0A;Vue&#x7EC4;&#x4EF6;&#xFF0C;&#x800C;context&#x5B9E;&#x4F8B;&#x5C31;&#x662F;&#x8FD9;&#x91CC;&#x7684;<code>EffectScope</code>&#x5BF9;&#x8C61;&#x4E86;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> scope = effectScope()
scope.run(() =&gt; {
  <span class="hljs-keyword">const</span> state = reactive({ value: <span class="hljs-number">1</span> })
  effect(() =&gt; {
    <span class="hljs-built_in">console</span>.log(state.value)
  })
})
scope.stop()
</code></pre>
<p>&#x90A3;&#x4E48;<code>effect</code>&#x751F;&#x6210;&#x7684;<code>ReactiveEffect</code>&#x5B9E;&#x4F8B;&#x662F;&#x5982;&#x4F55;&#x548C;scope&#x5173;&#x8054;&#x5462;&#xFF1F;
&#x90A3;&#x5C31;&#x662F;<code>ReactiveEffect</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;&#x7684;<code>recordEffectScope(this, scope)</code></p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">recordEffectScope</span>(<span class="hljs-params">
  effect: ReactiveEffect,
  scope?: EffectScope | <span class="hljs-literal">null</span>
</span>) </span>{
  <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x5C06;activeEffectScope&#x548C;&#x5F53;&#x524D;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7ED1;&#x5B9A;</span>
  scope = scope || activeEffectScope
  <span class="hljs-keyword">if</span> (scope &amp;&amp; scope.active) {
    scope.effects.push(effect)
  }
}
</code></pre>
<h2 id="&#x5C0F;&#x7ED3;"><a name="&#x5C0F;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x5C0F;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x5C0F;&#x7ED3;</h2>
<p>petite-vue&#x4E2D;&#x4F7F;&#x7528;<em>@vue/reactivity</em>&#x7684;&#x90E8;&#x5206;&#x7B97;&#x662F;&#x5256;&#x6790;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x4E5F;&#x8BB8;&#x4F60;&#x4F1A;&#x8BF4;<em>@vue/reactivity</em>&#x53EF;&#x4E0D;&#x6B62;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#x554A;&#xFF0C;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#x6211;&#x5C06;&#x4F1A;&#x5728;&#x540E;&#x7EED;&#x7684;&#x300A;vue-lit&#x6E90;&#x7801;&#x5256;&#x6790;&#x300B;&#x4E2D;&#x66F4;&#x8BE6;&#x5C3D;&#x7684;&#x68B3;&#x7406;&#x5206;&#x6790;&#xFF0C;&#x656C;&#x8BF7;&#x671F;&#x5F85;&#x3002;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:47:23
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="2_reactive_map_set.html" class="navigation navigation-prev " aria-label="Previous page: 逐行解读reactive如何构建Map和Set的响应式对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="4_cleanup_algorithm.html" class="navigation navigation-next " aria-label="Next page: 依赖清理优化算法详解-算法思路">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"逐行解读effect如何注册副作用函数","level":"3.4","depth":1,"next":{"title":"依赖清理优化算法详解-算法思路","level":"3.5","depth":1,"path":"2_reactivity_system/4_cleanup_algorithm.md","ref":"2_reactivity_system/4_cleanup_algorithm.md","articles":[]},"previous":{"title":"逐行解读reactive如何构建Map和Set的响应式对象","level":"3.3","depth":1,"path":"2_reactivity_system/2_reactive_map_set.md","ref":"2_reactivity_system/2_reactive_map_set.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"2_reactivity_system/3_register_side_effect.md","mtime":"2022-04-26T01:47:23.597Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

